package mongolo;

import Servicios.*;
import Biblioteca.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class agregarPeli extends javax.swing.JFrame {

    public agregarPeli() {
        initComponents();
        this.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
    }
    IMapeoObjetos mapo = new MapeoObjetos();

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        agregarPeli_BTN = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        agregarPeli_CodigoPeli = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        agregarPeli_NombrePeli = new javax.swing.JTextField();
        agregarPeli_Ano = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        agregarPeli_CantPelisDisponibles = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        agregarPeli_BTN.setText("Agregar pelicula");
        agregarPeli_BTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarPeli_BTNActionPerformed(evt);
            }
        });

        jLabel1.setText("Codigo");

        jLabel2.setText("Nombre de la pelicula");

        jLabel3.setText("Año");

        jLabel4.setText("Cantidad peliculas disponibles");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(agregarPeli_BTN))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(agregarPeli_CodigoPeli, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(agregarPeli_NombrePeli, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(agregarPeli_Ano, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(agregarPeli_CantPelisDisponibles, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(53, 53, 53))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(agregarPeli_CodigoPeli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(agregarPeli_NombrePeli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(agregarPeli_Ano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(agregarPeli_CantPelisDisponibles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addComponent(agregarPeli_BTN)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void agregarPeli_BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarPeli_BTNActionPerformed
        String codiPel = SM.ValidarNulls(agregarPeli_CodigoPeli.getText(), "Error");
        String nomPel = SM.ValidarNulls(agregarPeli_NombrePeli.getText(), "Error");
        String ano = SM.ValidarNulls(agregarPeli_Ano.getText(), "Error");
        int caPD = SM.ValidarInts(agregarPeli_CantPelisDisponibles.getText());

        ArrayList<Peli> LisPeli = mapo.getPeli(codiPel);
        if (caPD > 0) {
            if (LisPeli.size() > 0) {
                if (SM.cMes("El codigo de la peli ya existe, desea actualizarla?"))
                    if (mapo.UpdatePeli(codiPel, nomPel, ano, caPD))
                        JOptionPane.showMessageDialog(null, "Se actualizo la peli");
            } else 
                if( mapo.InsertPeli(codiPel, nomPel, ano, caPD))
                    JOptionPane.showMessageDialog(null, "Se inserto la peli");            
        } else {
            JOptionPane.showMessageDialog(null, "Error en la casilla cantidad de peliculas");
        }
    }//GEN-LAST:event_agregarPeli_BTNActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField agregarPeli_Ano;
    private javax.swing.JButton agregarPeli_BTN;
    private javax.swing.JTextField agregarPeli_CantPelisDisponibles;
    private javax.swing.JTextField agregarPeli_CodigoPeli;
    private javax.swing.JTextField agregarPeli_NombrePeli;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
